package com.travel.server.manager.impl.task;

import com.travel.server.manager.impl.youzan.YouzanSal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/22 10:54
 * @Desc:
 */
@Component
@Slf4j
public class ScheduledService {

    @Autowired
    private MysqlPoolTask mysqlPoolTask;

    @Autowired
    private OrderCompleteTask orderCompleteTask;

    @Scheduled(cron = "0 0 */2 * * ?")
    public void mysqlPoolTask(){
        Long time = System.currentTimeMillis();
        log.info("刷新mysql数据库连接池自动任务开始");
        mysqlPoolTask.handle();
        log.info("刷新mysql数据库连接池自动任务结束，用时" + (System.currentTimeMillis() - time) + "毫秒");
    }

    @Scheduled(cron = "0 0 1 * * ?")
    public void orderCompleteTask(){
        Long time = System.currentTimeMillis();
        log.info("订单到期自动任务开始");
        orderCompleteTask.handle();
        log.info("订单到期自动任务结束，用时" + (System.currentTimeMillis() - time) + "毫秒");
    }

    @Scheduled(cron = "0 32 16 ? * MON")
    public void refreshYouzanToken(){
        Long time = System.currentTimeMillis();
        log.info("自动刷新有赞token任务开始");
        YouzanSal.refreshToken();
        log.info("自动刷新有赞token任务结束，用时" + (System.currentTimeMillis() - time) + "毫秒");
    }

    @PostConstruct
    public void init(){
        log.info("启动后刷新有赞token");
        new Thread(){
            @Override
            public void run(){
                try{
                    YouzanSal.refreshToken();
                }catch (Exception e){
                    log.error("error exception {}", e.getMessage(), e);
                }
            }
        }.start();
    }
}
